package fr.efrei.international.java.assignment.two;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;

public class UsrRegister {

	DataLoader dl = null;
	String inputedUsr;
	String inputedPsw;
	
	public boolean checkUsr(String inputedUsr, String inputedPsw) {
		this.inputedUsr = inputedUsr;
		this.inputedPsw = inputedPsw;
		boolean flag = false;
		try {
			FileReader fr = new FileReader(".\\user account.txt");
			BufferedReader br = new BufferedReader(fr);
			String str1 = null;
			String[] s1 = null;
			while ((str1 = br.readLine()) != null) {
				s1 = str1.split("	");
				if ((s1[0].equals(inputedUsr)) && (s1[1].equals(inputedPsw))) {
					flag = true;
					break;
				}
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return flag;
	}

	public void creatNewUsr(String username, String passwordstr) {
		try {
			RandomAccessFile rf = new RandomAccessFile(".\\user account.txt",
					"rw");
			rf.seek(rf.length());
			rf.writeBytes(username);
			rf.writeBytes("	");
			rf.writeBytes(passwordstr + "\r\n");
			rf.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
